﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;
namespace eTruck.Views.Form
{
    public partial class Form : System.Web.UI.Page
    {
        public string Module, ProcessID, Action, FormID, ProcessName;
      public  eTruck.BLL.Form bll_Form ;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            bll_Form = new BLL.Form();
            Module = "";
            DataSet ds;
            if (Request.QueryString["ProcessID"] != null)
            {
                ProcessID = Request.QueryString["ProcessID"].ToString();               
                eTruck.BLL.Process bll_Process = new BLL.Process();
                ds = bll_Process.GetList(" ProcessID='" + ProcessID + "'");
                ProcessName = ds.Tables[0].Rows[0][1].ToString();
                BLL.Modules bll_Module = new BLL.Modules();
                ds = bll_Module.GetList(" a.ModuleName='" + ProcessName + "'");
                Module = ds.Tables[0].Rows[0][0].ToString();
            }
            else
            {
                string url = "../../Views/Users/Login.aspx";
                MessageBoxHelper.MessageBox((Page)Page.Parent.Parent, "请重新登陆", url);
            }
            if (Request.QueryString["FormID"] != null)
            {
                FormID = Request.QueryString["FormID"].ToString();
               // 显示FormInfo
              
            }
            Action = Request.QueryString["Action"].ToString();
           if(Action == "New")
            {
                Label_Status.Text = "新建" + ProcessName;
                
            }
            else
            {
                Button_Save.Text = ProcessName;
            }

            if (!IsPostBack)
            {
                HiddenField1.Value = "";
                HiddenField2.Value = "";
                HiddenField3.Value = "";

                eTruck.BLL.FormTypes bll_FormTypes = new eTruck.BLL.FormTypes();
                ds = bll_FormTypes.GetAllList();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ListItem _iPurpose = new ListItem();
                    _iPurpose.Text = ds.Tables[0].Rows[i][1].ToString();
                    _iPurpose.Value = ds.Tables[0].Rows[i][0].ToString();
                    DropDownList_Purpose.Items.Add(_iPurpose);
                }
                if( (FormID != "")&&(FormID != null)&&  (Action != "New"))
                {
                    show(FormID);
                }

            }
        }

        protected void show(string sFormID)
        {
            try
            {
                if (ProcessName != "车辆预约进厂")
                {
                    TextBox_CarNo.Enabled = false;
                    TextBox_DriverNo.Enabled = false;
                    TextBox_Reservation_In_Time.Enabled = false;
                }
                else
                {
                    Label_Status.Text = "修改车辆预约进厂";
                    Button_Save.Text = "修改车辆预约进厂";
                }
                if ((ProcessName != "车辆预约进厂") && (ProcessName != "车辆准备进厂"))
                {
                    CheckBox_CheckCar.Enabled = false;
                    CheckBox_Notice.Enabled = false;
                    TextBox_Contact.Enabled = false;
                    TextBox_Card.Enabled = false;
                    TextBox_Contact.Enabled = false;
                    DropDownList_Purpose.Enabled = false;
                    TextBox_OrderNumber.Enabled = false;

                }
                Model.Form sModelForm = new Model.Form();
                sModelForm = bll_Form.GetModel(Convert.ToInt32(FormID));
                ListItem LI = DropDownList_Purpose.Items.FindByValue(sModelForm.FormTypeID.ToString());
                DropDownList_Purpose.Text = LI.Text;
                Model.Car sCar = new Model.Car();
                BLL.Car bll_Car = new BLL.Car();
                sCar = bll_Car.GetModel(sModelForm.CarID);
                TextBox_CarNo.Text = sCar.Car_NO;
                TextBox_Capaity.Text = sCar.Capacity.ToString() + " " + sCar.Unit.ToString();
                Model.Supplier sSupplier = new Model.Supplier();
                BLL.Supplier bll_Supplier = new BLL.Supplier();
                sSupplier = bll_Supplier.GetModel(Convert.ToInt32(sCar.CorporationID));
                TextBox_Crop.Text = sSupplier.CorporationName;
                TextBox_Crop_People.Text = sSupplier.Contact;
                TextBox_Contact.Text = sModelForm.Contact;
                Model.Driver sDriver = new Model.Driver();
                BLL.Driver bll_Driver = new BLL.Driver();
                sDriver = bll_Driver.GetModel(sModelForm.DriverID);
                TextBox_DriverNo.Text = sDriver.CertificationNO;
                TextBox_DriverName.Text = sDriver.DriverName;
                if (sModelForm.CardID != null)
                {
                    TextBox_Card.Text = sModelForm.CardID;
                }
                if (sModelForm.OrderNumber!=null)
                {
                TextBox_OrderNumber.Text = sModelForm.OrderNumber;
                }
                CheckBox_CheckCar.Checked = sModelForm.CheckedCar;
                CheckBox_Notice.Checked = sModelForm.Noticed;
                if (sModelForm.Reservation_In_Time != null)
                {
                    TextBox_Reservation_In_Time.Text = sModelForm.Reservation_In_Time.ToString();
                }
                DropDownList_Purpose.SelectedValue = sModelForm.FormTypeID.ToString();
                BLL.FormHistory bllFormHistory = new BLL.FormHistory();
                DataSet ds = bllFormHistory.GetHistoryByProcess(FormID, "车辆进厂");
                if (ExistsData(ds))
                {
                    TextBox_SC_In.Text = ds.Tables[0].Rows[0][2].ToString();
                    TextBox_In_Time.Text = ds.Tables[0].Rows[0][3].ToString();
                }
                ds = bllFormHistory.GetHistoryByProcess(FormID, "车辆出厂");
                if (ExistsData(ds))
                {
                    TextBox_WH_In.Text = ds.Tables[0].Rows[0][2].ToString();
                    TextBox_WH_Time.Text = ds.Tables[0].Rows[0][3].ToString();
                }
                ds = bllFormHistory.GetHistoryByProcess(FormID, "车辆预约进厂");
                if (ExistsData(ds))
                {
                    TextBox_Reservation.Text = ds.Tables[0].Rows[0][2].ToString();
                    //TextBoxReservation_Time.Text = ds.Tables[0].Rows[0][3].ToString();
                }
                BLL.Route bll_Route = new BLL.Route();
                ds = bll_Route.GetList("FormTypeID='" + LI.Value + "' and ProcessID='" + sModelForm.Processid.ToString() + "'");
                if (ExistsData(ds))
                {
                    string NextProcessID = ds.Tables[0].Rows[0][2].ToString();
                    BLL.Process bll_Process = new BLL.Process();
                    Model.Process model_Process = bll_Process.GetModel(Convert.ToInt32(NextProcessID));
                    Label_Status.Text = "待-" + model_Process.ProcessName;
                }
            }
            catch
            {
            }

        }
        protected Boolean ExistsData(DataSet ds)
        {
            if (ds == null)
            {
                return false;
            }
            if (ds.Tables.Count == 0)
            {
                return false;
            }
            if (ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            return true;
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected Boolean CheckInput(string sAction, string Process)
        {

            if (TextBox_CarNo.Text == "")
            {
                HiddenField1.Value = "输入信息";
                HiddenField2.Value = "没有输入车牌号";
                HiddenField3.Value = "error";
                return false;
            }
            if (TextBox_Contact.Text == "")
            {
                HiddenField1.Value = "输入信息";
                HiddenField2.Value = "没有输入联系人";
                HiddenField3.Value = "error";
                return false;
            }
            if (TextBox_DriverNo.Text == "")
            {
                HiddenField1.Value = "输入信息";
                HiddenField2.Value = "没有输入驾驶证号";
                HiddenField3.Value = "error";
                return false;
            }

            if (Process == "车辆预约进厂")
            {
                DateTime dtm;
                if (!DateTime.TryParse(TextBox_Reservation_In_Time.Text, out dtm))
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "预约时间输入错误！";
                    HiddenField3.Value = "error";
                    return false;
                }
                if (dtm < DateTime.Now)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "预约时间小于当前时间";
                    HiddenField3.Value = "error";
                    return false;
                }
            }
            else
            {
                if (!CheckBox_CheckCar.Checked)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "请先检查车辆情况";
                    HiddenField3.Value = "error";
                    return false;
                }
                if (!CheckBox_Notice.Checked)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "请先让司机阅读注意事项";
                    HiddenField3.Value = "error";
                    return false;
                }
               
                if (TextBox_Card.Text == "")
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "请输入通行卡号";
                    HiddenField3.Value = "error";
                    return false;
                }
            }


            return true;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //检查输入信息
              eTruck.Model.Form model_Form=new Model.Form() ;
              if (!CheckInput("New", ProcessName))
              {
                  return;
              }
            if (Action == "New")
            {
                HiddenField1.Value = "";
                HiddenField2.Value = "";
                HiddenField3.Value = "";
               
                
                string FormTypeID = DropDownList_Purpose.SelectedItem.Value.ToString();
                eTruck.BLL.Process bll_Process = new BLL.Process();
                DataSet ds;
                ds = bll_Process.GetList("ProcessName='" + ProcessName.Replace("'", "") + "'");
                if (ds == null)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到流程:" + ProcessName;
                    HiddenField3.Value = "error";
                    return;
                }
                if (ds.Tables.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到流程:" + ProcessName;
                    HiddenField3.Value = "error";
                    return;
                }
                if (ds.Tables[0].Rows.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到流程:" + ProcessName;
                    HiddenField3.Value = "error";
                    return;
                }
                string ProcessID = ds.Tables[0].Rows[0][0].ToString();
               
               
                model_Form.CarID = Convert.ToInt32(Label_CarID.Text);
                model_Form.DriverID = Convert.ToInt32(Label_DriverID.Text);
                model_Form.FormTypeID = Convert.ToInt32(FormTypeID);
                model_Form.Contact = TextBox_Contact.Text;
               
                    model_Form.CardID = TextBox_Card.Text;
                
                model_Form.Processid = Convert.ToInt32(ProcessID);
                model_Form.OrderNumber = TextBox_OrderNumber.Text;
                
                model_Form.Noticed = false;
                model_Form.CheckedCar = false;
                if (ProcessName == "车辆预约进厂")
                {
                    model_Form.Reservation_In_Time = DateTime.Parse(TextBox_Reservation_In_Time.Text);
                }
                if (CheckBox_Notice.Checked)
                {
                    model_Form.Noticed = true;
                }
                if (CheckBox_CheckCar.Checked)
                {
                    model_Form.CheckedCar = true;
                }
                int _formid = bll_Form.Add(model_Form);
                if (_formid != 0)
                {
                    if (ProcessName == "车辆预约进厂")
                    {
                        HiddenField1.Value = "添加预约";
                        HiddenField2.Value = "添加预约成功";
                        HiddenField3.Value = "info";
                    }
                    else
                    {
                        HiddenField1.Value = "添加车辆准备进厂";
                        HiddenField2.Value = "添加车辆准备进厂成功";
                        HiddenField3.Value = "info";
                    }
                  
                    //添加History;
                    BLL.FormHistory bll_FormHistory = new BLL.FormHistory();
                    Model.FormHistory model_History = new Model.FormHistory();
                    model_History.FormID = _formid;
                    model_History.ProcessID = Convert.ToInt32(ProcessID);
                    model_History.UserID =Convert.ToInt32( CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
                    model_History.Lastupdated = DateTime.Now;
                    bll_FormHistory.Add(model_History);
                    Response.Redirect("FormQueue.aspx?Module=" + Module);
                }
                else
                {
                    if (ProcessName == "车辆预约进厂")
                    {
                        HiddenField1.Value = "添加预约";
                        HiddenField2.Value = "添加预约出错";
                        HiddenField3.Value = "error";
                    }
                    else
                    {
                        HiddenField1.Value = "添加车辆准备进厂";
                        HiddenField2.Value = "添加车辆准备进厂出错";
                        HiddenField3.Value = "error";
                    }
                }
            }
             else //审批
            {
                model_Form = bll_Form.GetModel(Convert.ToInt32(FormID));
                if (ProcessName == "车辆进厂")
                {
                    model_Form.InDatetime = DateTime.Now;
                }
                if (ProcessName == "车辆出厂")
                {
                    model_Form.OutDatetime = DateTime.Now;
                }
                model_Form.Processid=Convert.ToInt32(ProcessID);
                model_Form.Noticed = CheckBox_Notice.Checked;
                model_Form.CheckedCar = CheckBox_CheckCar.Checked;
                model_Form.Contact = TextBox_Contact.Text;
                model_Form.CardID = TextBox_Card.Text;
                bll_Form.Update(model_Form);
                BLL.FormHistory bll_FormHistory = new BLL.FormHistory();
                Model.FormHistory model_History = new Model.FormHistory();
                model_History.FormID = Convert.ToInt32(FormID);
                model_History.ProcessID = Convert.ToInt32(ProcessID);
                model_History.UserID =Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
                
                model_History.Lastupdated = DateTime.Now;
                bll_FormHistory.Add(model_History);
                Response.Redirect("FormQueue.aspx?Module=" + Module);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("FormQueue.aspx?Module=" + Module); 
        }

        protected void TextBox_CarNo_TextChanged(object sender, EventArgs e)
        {
            if (TextBox_CarNo.Text != "")
            {
                string CarNO = TextBox_CarNo.Text.Replace("'", "").Trim();
                eTruck.BLL.Car bll_Car = new eTruck.BLL.Car();
                DataSet ds = bll_Car.GetList("Car_NO='" + CarNO + "'");
                HiddenField1.Value = "";
                HiddenField2.Value = "";
                HiddenField3.Value = "";
                if (ds == null)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到车牌号:" + CarNO;
                    HiddenField3.Value = "error";
                    TextBox_CarNo.Text = "";
                    return;
                }
                if (ds.Tables.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到车牌号:" + CarNO;
                    HiddenField3.Value = "error";
                    TextBox_CarNo.Text = "";
                    return;
                }
                if (ds.Tables[0].Rows.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到车牌号:" + CarNO;
                    TextBox_CarNo.Text = "";
                    HiddenField3.Value = "error";
                    return;
                }
                Label_CarID.Text = ds.Tables[0].Rows[0][0].ToString();
                string CorporationID = ds.Tables[0].Rows[0][2].ToString();
                TextBox_Capaity.Text = ds.Tables[0].Rows[0][3].ToString() + " " + ds.Tables[0].Rows[0][4].ToString();
                eTruck.BLL.Supplier bll_Supplier = new BLL.Supplier();

                ds = bll_Supplier.GetList("CorporationID='" + CorporationID + "'");
                TextBox_Crop.Text = ds.Tables[0].Rows[0][1].ToString();
                TextBox_Crop_People.Text = ds.Tables[0].Rows[0][3].ToString();
                Label_CroprationID.Text = ds.Tables[0].Rows[0][0].ToString();
               
            }
           
        }

        protected void TextBox_DriverNo_TextChanged(object sender, EventArgs e)
        {
            if (TextBox_DriverNo.Text != "")
            {
                string CertificationNO = TextBox_DriverNo.Text.Replace("'", "").Trim();
                eTruck.BLL.Driver bll_Driver = new eTruck.BLL.Driver();
                DataSet ds = bll_Driver.GetList("CertificationNO='" + CertificationNO + "'");
                HiddenField1.Value = "";
                HiddenField2.Value = "";
                HiddenField3.Value = "";
                if (ds == null)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到驾驶证号:" + CertificationNO;
                    HiddenField3.Value = "error";
                    TextBox_DriverNo.Text = "";
                    return;
                }
                if (ds.Tables.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到驾驶证号:" + CertificationNO;
                    HiddenField3.Value = "error";
                    TextBox_DriverNo.Text = "";
                    return;
                }
                if (ds.Tables[0].Rows.Count == 0)
                {
                    HiddenField1.Value = "输入信息";
                    HiddenField2.Value = "找不到驾驶证号:" + CertificationNO;
                    HiddenField3.Value = "error";
                    TextBox_DriverNo.Text = "";
                    return;
                }
                TextBox_DriverName.Text = ds.Tables[0].Rows[0][1].ToString();
                Label_DriverID.Text = ds.Tables[0].Rows[0][0].ToString();
                }
        }

        protected void DropDownList_Purpose_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}